#include "cell.h"
#include "ht32.h"
#include "string.h"
#include "cell_pwm.h"
#include "delay.h"

cell_inf_t cell_1_inf,cell_2_inf;
esp_inf_t esp_get;
uint8_t esp_rx_buf[RX_BUFF_LONG] = {0};
uint8_t check_RX;
uint8_t rx_data_long = 0;
uint8_t check_1_init_flag = 0 ;
uint8_t check_2_init_flag = 0 ;
void init_cell_fun(void){
	memcpy(&cell_1_inf.pin_pwm_u,&cell_1_pwm_param_u,sizeof(cell_pwm_t));
    memcpy(&cell_1_inf.pin_pwm_d,&cell_1_pwm_param_d,sizeof(cell_pwm_t));
    memcpy(&cell_2_inf.pin_pwm_u,&cell_2_pwm_param_u,sizeof(cell_pwm_t));
    memcpy(&cell_2_inf.pin_pwm_d,&cell_2_pwm_param_d,sizeof(cell_pwm_t));
	
    cell_1_inf.cell_name = CELL1;
    cell_2_inf.cell_name = CELL2;
	
	cell_1_inf.set_pin = TM_SetCaptureCompare;

	cell_2_inf.set_pin = TM_SetCaptureCompare;

	
	delay_ms(100);
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_0,cell_1_inf.pin_pwm_d.pin1_pwm);//0 G0T
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_1,cell_1_inf.pin_pwm_d.pin2_pwm);//1 G0T
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_2,cell_1_inf.pin_pwm_d.pin3_pwm);//2 G0T
	delay_ms(100);
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_3,cell_1_inf.pin_pwm_d.pin4_pwm);//3 G0T
		cell_1_inf.set_pin(CELL1_2_TIM,TM_CH_0,cell_1_inf.pin_pwm_d.pin5_pwm);//4 G1T
		cell_1_inf.set_pin(CELL1_2_TIM,TM_CH_1,cell_1_inf.pin_pwm_d.pin6_pwm);//5 G1T
	delay_ms(100);
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_0,cell_2_inf.pin_pwm_d.pin1_pwm);//0 MOT
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_1,cell_2_inf.pin_pwm_d.pin2_pwm);//1 MOT
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_2,cell_2_inf.pin_pwm_d.pin3_pwm);//2 MOT
	delay_ms(100);
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_3,cell_2_inf.pin_pwm_d.pin4_pwm);//3 MOT
		cell_2_inf.set_pin(CELL1_2_TIM,TM_CH_2,cell_2_inf.pin_pwm_d.pin5_pwm);//4  G1T
		cell_2_inf.set_pin(CELL1_2_TIM,TM_CH_3,cell_2_inf.pin_pwm_d.pin6_pwm);//5  G1T
	delay_ms(100);
	
	
	
	
	
	
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_0,cell_1_inf.pin_pwm_u.pin1_pwm);//0 G0T
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_1,cell_1_inf.pin_pwm_u.pin2_pwm);//1 G0T
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_2,cell_1_inf.pin_pwm_u.pin3_pwm);//2 G0T
	delay_ms(100);
		cell_1_inf.set_pin(CELL1_TIM,TM_CH_3,cell_1_inf.pin_pwm_u.pin4_pwm);//3 G0T
		cell_1_inf.set_pin(CELL1_2_TIM,TM_CH_0,cell_1_inf.pin_pwm_u.pin5_pwm);//4 G1T
		cell_1_inf.set_pin(CELL1_2_TIM,TM_CH_1,cell_1_inf.pin_pwm_u.pin6_pwm);//5 G1T
	delay_ms(100);
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_0,cell_2_inf.pin_pwm_u.pin1_pwm);//0 MOT
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_1,cell_2_inf.pin_pwm_u.pin2_pwm);//1 MOT
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_2,cell_2_inf.pin_pwm_u.pin3_pwm);//2 MOT
	delay_ms(100);
		cell_2_inf.set_pin(CELL2_TIM,TM_CH_3,cell_2_inf.pin_pwm_u.pin4_pwm);//3 MOT
		cell_2_inf.set_pin(CELL1_2_TIM,TM_CH_2,cell_2_inf.pin_pwm_u.pin5_pwm);//4  G1T
		cell_2_inf.set_pin(CELL1_2_TIM,TM_CH_3,cell_2_inf.pin_pwm_u.pin6_pwm);//5  G1T
	delay_ms(100);
	check_1_init_flag = 1;
	check_2_init_flag = 1;
}

void get_cell_inf(void){
    if (esp_get.head == ESP_HEAD && esp_get.tail == ESP_TAIL){
        if(esp_get.HT_CH == HT_NOW_CHAH &&esp_get._2_HT_CH== HT_NOW_CHAH){
            if(esp_get.CELL_CH == 0X00&&esp_get._2_CELL_CH == 0X01){
                cell_1_inf.pin1_status = esp_get.pin_0;
                cell_1_inf.pin2_status = esp_get.pin_1;
                cell_1_inf.pin3_status = esp_get.pin_2;
                cell_1_inf.pin4_status = esp_get.pin_3;
                cell_1_inf.pin5_status = esp_get.pin_4;
                cell_1_inf.pin6_status = esp_get.pin_5;
				
				cell_2_inf.pin1_status = esp_get._2_pin_0;
				cell_2_inf.pin2_status = esp_get._2_pin_1;
				cell_2_inf.pin3_status = esp_get._2_pin_2;
				cell_2_inf.pin4_status = esp_get._2_pin_3;
				cell_2_inf.pin5_status = esp_get._2_pin_4;
				cell_2_inf.pin6_status = esp_get._2_pin_5;
			}
        }
    }
}

void set_cell_inf(cell_inf_t *cell_go){
    if(cell_go->cell_name ==CELL1 && check_1_init_flag == 1){
		if(cell_go->pin1_status == UP_){
			cell_go->set_pin(CELL1_TIM,TM_CH_0,cell_go->pin_pwm_u.pin1_pwm);
		}
		else if (cell_go->pin1_status == DO_ ){
			cell_go->set_pin(CELL1_TIM,TM_CH_0,cell_go->pin_pwm_d.pin1_pwm);
		}
		delay_ms(100);
		
		if(cell_go->pin2_status == UP_){
			cell_go->set_pin(CELL1_TIM,TM_CH_1,cell_go->pin_pwm_u.pin2_pwm);
		}
		else if (cell_go->pin2_status == DO_ ){
			cell_go->set_pin(CELL1_TIM,TM_CH_1,cell_go->pin_pwm_d.pin2_pwm);
		}
		delay_ms(100);
		
		if(cell_go->pin3_status == UP_){
			cell_go->set_pin(CELL1_TIM,TM_CH_2,cell_go->pin_pwm_u.pin3_pwm);
		}
		else if (cell_go->pin3_status == DO_ ){
			cell_go->set_pin(CELL1_TIM,TM_CH_2,cell_go->pin_pwm_d.pin3_pwm);
		}
		delay_ms(100);
		
		if(cell_go->pin4_status == UP_){
			cell_go->set_pin(CELL1_TIM,TM_CH_3,cell_go->pin_pwm_u.pin4_pwm);
		}
		else if (cell_go->pin4_status == DO_ ){
			cell_go->set_pin(CELL1_TIM,TM_CH_3,cell_go->pin_pwm_d.pin4_pwm);
		}
		delay_ms(100);
		
		if(cell_go->pin5_status == UP_){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_0,cell_go->pin_pwm_u.pin5_pwm);
		}
		else if (cell_go->pin5_status == DO_ ){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_0,cell_go->pin_pwm_d.pin5_pwm);
		}
		delay_ms(100);
		if(cell_go->pin6_status == UP_){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_1,cell_go->pin_pwm_u.pin6_pwm);
		}
		else if (cell_go->pin6_status == DO_ ){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_1,cell_go->pin_pwm_d.pin6_pwm);
		}
    }
	/* -----------------------------------------------------------------------------------------*/
    else if (cell_go->cell_name == CELL2 && check_2_init_flag == 1){
		if(cell_go->pin1_status == UP_){
			cell_go->set_pin(CELL2_TIM,TM_CH_0,cell_go->pin_pwm_u.pin1_pwm);
		}
		else if (cell_go->pin1_status == DO_ ){
			cell_go->set_pin(CELL2_TIM,TM_CH_0,cell_go->pin_pwm_d.pin1_pwm);
		}
		delay_ms(100);
		if(cell_go->pin2_status == UP_){
			cell_go->set_pin(CELL2_TIM,TM_CH_1,cell_go->pin_pwm_u.pin2_pwm);
		}
		else if (cell_go->pin2_status == DO_ ){
			cell_go->set_pin(CELL2_TIM,TM_CH_1,cell_go->pin_pwm_d.pin2_pwm);
		}
		delay_ms(100);
		if(cell_go->pin3_status == UP_){
			cell_go->set_pin(CELL2_TIM,TM_CH_2,cell_go->pin_pwm_u.pin3_pwm);
		}
		else if (cell_go->pin3_status == DO_ ){
			cell_go->set_pin(CELL2_TIM,TM_CH_2,cell_go->pin_pwm_d.pin3_pwm);
		}
		delay_ms(100);
		if(cell_go->pin4_status == UP_){
			cell_go->set_pin(CELL2_TIM,TM_CH_3,cell_go->pin_pwm_u.pin4_pwm);
		}
		else if (cell_go->pin4_status == DO_ ){
			cell_go->set_pin(CELL2_TIM,TM_CH_3,cell_go->pin_pwm_d.pin4_pwm);
		}
		delay_ms(100);
		
		if(cell_go->pin5_status == UP_){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_2,cell_go->pin_pwm_u.pin5_pwm);
		}
		else if (cell_go->pin5_status == DO_ ){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_2,cell_go->pin_pwm_d.pin5_pwm);
		}
		delay_ms(100);
		if(cell_go->pin6_status == UP_){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_3,cell_go->pin_pwm_u.pin6_pwm);
		}
		else if (cell_go->pin6_status == DO_ ){
			cell_go->set_pin(CELL1_2_TIM,TM_CH_3,cell_go->pin_pwm_d.pin6_pwm);
		}
    }
    else return;
}

